package cn.demon.user.controller;


import cn.demon.basic.exception.BusinessException;
import cn.demon.basic.util.AjaxResult;
import cn.demon.user.dto.BinderDto;
import cn.demon.user.dto.LoginDto;
import cn.demon.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 账号登录接口
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map = logininfoService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试!!!");
        }

    }

    /**
     * 微信登录接口
     * @param code
     * @return
     */
    @GetMapping("/wechat/{code}")
    public AjaxResult wechatLogin(@PathVariable("code") String code){
        try {
            return logininfoService.wechatLogin(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试!!!");
        }
    }

    /**
     * 微信绑定接口
     * @param binderDto
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody BinderDto binderDto){
        try {
            return logininfoService.wechatBinder(binderDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试!!!");
        }
    }

    /**
     * 退出登录接口
     * @param token
     * @return
     */
    @GetMapping("/logout/{token}")
    public AjaxResult logout(@PathVariable("token") String token){
        try {
            logininfoService.logout(token);
            return new AjaxResult();
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试!!!");
        }

    }
}
